home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 06 General Architectures / 06 Rabin / time.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-12-10  |  1.4 KB  |  67 lines

  1. /* Copyright (C) Steve Rabin, 2001. 
  2.  * All rights reserved worldwide.
  3.  *
  4.  * This software is provided "as is" without express or implied
  5.  * warranties. You may freely copy and compile this source into
  6.  * applications you distribute provided that the copyright text
  7.  * below is included in the resulting source code, for example:
  8.  * "Portions Copyright (C) Steve Rabin, 2001"
  9.  */
  10.  
  11. #include "time.h"
  12. #include <windows.h>
  13. #include <assert.h>
  14.  
  15. #define INITIAL_GAME_SPEED 1.0f
  16. #define MIN_GAME_SPEED 0.001f
  17. #define MAX_GAME_SPEED 100.0f
  18.  
  19.  
  20. Time::Time( void )
  21. {
  22.     m_CurrentTime = 0.0f;
  23.     m_TimeLastTick = 0.001f;
  24.     m_GameSpeed = INITIAL_GAME_SPEED;
  25.  
  26.     m_StartTime = ( GetExactTimeInMilliseconds() * INITIAL_GAME_SPEED ) / 1000.0f;
  27.  
  28. }
  29.  
  30.  
  31. void Time::MarkTimeThisTick( void )
  32. {
  33.     float newTime = ( ( GetExactTimeInMilliseconds() * m_GameSpeed ) / 1000.0f ) - m_StartTime;
  34.  
  35.     m_TimeLastTick = newTime - m_CurrentTime;
  36.     m_CurrentTime = newTime;
  37.  
  38.     if( m_TimeLastTick <= 0.0f ) {
  39.         m_TimeLastTick = 0.001f;
  40.     }
  41.  
  42. }
  43.  
  44.  
  45. void Time::SetGameSpeed( float value )
  46. {
  47.     assert( value >= MIN_GAME_SPEED && value <= MAX_GAME_SPEED && "Time::SetGameSpeed - out of range" );
  48.  
  49.     m_GameSpeed = value;
  50.  
  51.     if( m_GameSpeed < MIN_GAME_SPEED ) {
  52.         m_GameSpeed = MIN_GAME_SPEED;
  53.     }
  54.     else if ( m_GameSpeed > MAX_GAME_SPEED ) {
  55.         m_GameSpeed = MIN_GAME_SPEED;
  56.     }
  57.  
  58. }
  59.  
  60.  
  61. unsigned int Time::GetExactTimeInMilliseconds( void )
  62. {
  63.     return( timeGetTime() );
  64. }
  65.  
  66.  
  67.